package cc.linker.steplink.crm.dao;

import cc.linker.steplink.crm.domain.ArticleRule;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface ArticleRuleDao {
    ArticleRule getArticleRule(String orgId) throws Exception;

    int addArticleRule(ArticleRule record) throws Exception;

    int ifExist(String orgId) throws Exception;

    @Select("SELECT\n" +
            "\tORG_ID orgId,\n" +
            "\t`CONDITION` `condition`,\n" +
            "\tIF_LIKE ifLike,\n" +
            "\tIF_COMMENT ifComment,\n" +
            "\tREAD_TIME_MIN readTimeMin,\n" +
            "\tREAD_TIME_MAX readTimeMax,\n" +
            "\tOPEN_MIN openMin,\n" +
            "\tOPEN_MAX openMax,\n" +
            "\tREAD_TIMES_MIN readTimesMin,\n" +
            "\tREAD_TIMES_MAX readTimesMax \n" +
            "FROM\n" +
            "\tt_crm_busi_article_rule\n" +
            "WHERE\n" +
            "\t`CONDITION` != '3' ")
    List<Map<String, Object>> listAll();

    @Select("<script>SELECT\n" +
            "\tcustomerId,\n" +
            "\tGROUP_CONCAT(DISTINCT articleLabels) articleLabels\n" +
            "FROM\n" +
            "\t`v_busi_article_label` \n" +
            "WHERE\n" +
            "\torgId = #{orgId}\n" +
            "\t<if test='openMin != null and openMax!= null'>\n" +
            "\tAND `open` BETWEEN #{openMin} AND #{openMax} \n" +
            "\t</if>\n" +
            "\t<if test='readTimesMin != null and readTimesMax != null'>\n" +
            "\tAND `read` BETWEEN #{readTimesMin} AND #{readTimesMax} \n" +
            "\t</if>\n" +
            "\t<if test='ifComment == 1' >\n" +
            "\tAND `comment` > 0 \n" +
            "\t</if>\n" +
            "\t<if test='ifLike == 1' >\n" +
            "\tAND `like` > 0 \n" +
            "\t</if>\n" +
            "\t<if test='readTimeMin != null and readTimeMax != null'>\n" +
            "\tAND `time` BETWEEN #{readTimeMin} AND #{readTimeMax} \n" +
            "\t</if>\n" +
            "\tGROUP BY customerId</script>")
    List<Map<String, Object>> getArticleLabels(Map<String, Object> map);

    @Select("<script>SELECT\n" +
            "\tcustomerId,\n" +
            "\tGROUP_CONCAT(DISTINCT articleLabels) articleLabels\n" +
            "FROM\n" +
            "\t`v_busi_article_label` \n" +
            "WHERE\n" +
            "\torgId = #{orgId}\n" +
            "\t<trim prefix='AND(' suffix=')' prefixOverrides='OR' >\n" +
            "\t<if test='openMin != null and openMax!= null'>\n" +
            "\tOR `open` BETWEEN #{openMin} AND #{openMax} \n" +
            "\t</if>\n" +
            "\t<if test='readTimesMin != null and readTimesMax != null'>\n" +
            "\tOR `read` BETWEEN #{readTimesMin} AND #{readTimesMax} \n" +
            "\t</if>\n" +
            "\t<if test='ifComment == 1' >\n" +
            "\tOR `comment` > 0 \n" +
            "\t</if>\n" +
            "\t<if test='ifLike == 1' >\n" +
            "\tOR `like` > 0 \n" +
            "\t</if>\n" +
            "\t<if test='readTimeMin != null and readTimeMax != null'>\n" +
            "\tOR `time` BETWEEN #{readTimeMin} AND #{readTimeMax} \n" +
            "\t</if>\n" +
            "\t</trim>\n" +
            "\tGROUP BY customerId</script>")
    List<Map<String, Object>> getArticleLabelsSelective(Map<String, Object> map);

    @Select("SELECT DISTINCT\n" +
            "\tLABEL_ID \n" +
            "FROM\n" +
            "\tt_crm_tbl_label \n" +
            "WHERE\n" +
            "\tFIND_IN_SET( LABEL_NAME, ( #{labels} ) ) \n" +
            "\tAND ORG_ID = #{orgId} ")
    List<String> getLabelIds(Map<String, Object> selectMap);

    @Insert("<script>INSERT IGNORE t_crm_busi_label\n" +
            "(LABEL_ID,CUSTOMER_ID)\n" +
            "VALUES\n" +
            "<foreach collection='labelIds' item='item' separator=',' >\n" +
            " (#{item}, #{customerId}) \n" +
            "</foreach></script>")
    void insertToLabel(Map<String, Object> insertMap);
}
